package com.ruoyi.system.listener;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.LabinfoItemData;
import com.ruoyi.system.mapper.LabinfoItemDataMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@RocketMQMessageListener(topic = "LIMSPROCESS",consumerGroup = "BIS_ZHSZ",selectorExpression = "PROCESS")
@Slf4j
public class RocketmqListener implements RocketMQListener<String> {

    @Autowired
    private LabinfoItemDataMapper labinfoItemDataMapper;

    @SneakyThrows
    @Override
    public void onMessage(String s) {
        JSONArray jsonList=JSONObject.parseArray(s);
        for (int i = 0; i < jsonList.size(); i++) {
            JSONObject jsonObjectForUpdate= jsonList.getJSONObject(i);
            String sampleBoxCode = jsonObjectForUpdate.getString("sample_box_code");
            String limsSchedule=jsonObjectForUpdate.getString("modual");
            log.info("Rocket更新进度："+ sampleBoxCode +"："+ limsSchedule);
            LabinfoItemData labinfoItemData = labinfoItemDataMapper.getItemBySampleBoxCode(sampleBoxCode);
            if(labinfoItemData != null) { //不为空,则更新
                if(StringUtils.isEmpty(limsSchedule)){
                    limsSchedule = "收样";
                }
                labinfoItemData.setLimsSchedule(limsSchedule);
                String reportAddress = labinfoItemData.getReportAddress();
                if(StringUtils.isNotEmpty(reportAddress)){ //不为空
                    labinfoItemData.setReportSchedule("2");//已开始
                }else{
                    labinfoItemData.setReportSchedule("1"); //检测中
                }
                labinfoItemData.setSampleBoxCode(sampleBoxCode);
                labinfoItemDataMapper.updateReportSchedule(labinfoItemData);
            }
        }
    }
}
